<?php 

class My_AuthAdapter implements Zend_Auth_Adapter_Interface {
	
	protected $_login = null;
	protected $_password = null;
	
	protected $_user =null;
	protected $_resultInfo = array("code"=>null,"identity"=>null,"messages"=>array());
	
    public function __construct(){
    	
    }
    
    public function setLogin($login){
    	$this->_login = $login;
        return $this;
    }
    
	public function setPassword($password){
    	$this->_password = $password;
        return $this;
    }
    	
	public function authenticate(){
		if($this->main_test()){
			$this->after_math();
		}
		return $this->createAuthResult();
    }
    
    private function main_test(){
		
		$user = new User();
		$this->_user = $user->fetchRow(Utils::quote("user_login = ?",$this->_login));
		
		if($this->_user){
			if($this->_user->user_active != 1){
				$this->_resultInfo['identity'] = null;
    			$this->_resultInfo['messages'] = array("login active error");
    			$this->_resultInfo["code"] = 0;
    			return false;
			} else {
				$this->_resultInfo['identity'] = $this->_user->user_id;
	    		$this->_resultInfo['messages'] = array("login ok");
	    		$this->_resultInfo["code"] = 1;
	    		return true;
			}
		} else {
			$this->_resultInfo['identity'] = null;
    		$this->_resultInfo['messages'] = array("login main error");
    		$this->_resultInfo["code"] = 0;
    		return false;
		}
    }

    private function after_math(){
        if ($this->_resultInfo['code'] == 1){
			$_SESSION['user'] =  $this->_user;
    	}
    }

    protected function createAuthResult(){
		return new Zend_Auth_Result(
			$this->_resultInfo['code'],
			$this->_resultInfo['identity'],
			$this->_resultInfo['messages']
		);
    }

}


?>